#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class TeacherProvider : CY.GFive.Core.Providers.DALProvider.ITeacherProvider
	{
		#region Sql
		private static readonly string SqlSelectTeacher = "SELECT [Id], [Code],[Name],[Gender],[CatetoryCode],[Age],[Salary],[Degree],[Record],[Pic] FROM [Teacher] WHERE  [Id]=@Id";

		private static readonly string SqlInsertTeacher = "INSERT INTO [Teacher]([Code],[Name],[Gender],[CatetoryCode],[Age],[Salary],[Degree],[Record],[Pic]) values(@Code,@Name,@Gender,@CatetoryCode,@Age,@Salary,@Degree,@Record,@Pic);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateTeacher = "UPDATE [Teacher] SET [Code] = @Code,[Name] = @Name,[Gender] = @Gender,[CatetoryCode] = @CatetoryCode,[Age] = @Age,[Salary] = @Salary,[Degree] = @Degree,[Record] = @Record,[Pic] = @Pic WHERE  [Id]=@Id";

		private static readonly string SqlDeleteTeacher = "DELETE [Teacher] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllTeacher = "SELECT [Id], [Code],[Name],[Gender],[CatetoryCode],[Age],[Salary],[Degree],[Record],[Pic] FROM [Teacher]";
		#endregion

		#region ITeacherProvider Members
		public Core.Business.Teacher Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectTeacher);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.Teacher teacher = new Core.Business.Teacher();

				if (!reader.IsDBNull(0)) teacher.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) teacher.Code = reader.GetString(1);
				if (!reader.IsDBNull(2)) teacher.Name = reader.GetString(2);
				if (!reader.IsDBNull(3)) teacher.Gender = reader.GetByte(3);
				if (!reader.IsDBNull(4)) teacher.CatetoryCode = reader.GetString(4);
				if (!reader.IsDBNull(5)) teacher.Age = reader.GetByte(5);
				if (!reader.IsDBNull(6)) teacher.Salary = reader.GetDecimal(6);
				if (!reader.IsDBNull(7)) teacher.Degree = reader.GetString(7);
				if (!reader.IsDBNull(8)) teacher.Record = reader.GetString(8);
				if (!reader.IsDBNull(9)) teacher.Pic = reader.GetString(9);

				return teacher;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.Teacher teacher)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Code", SqlDbType.NVarChar, teacher.Code);
			sql.AddParameter("@Name", SqlDbType.NVarChar, teacher.Name);
			sql.AddParameter("@Gender", SqlDbType.TinyInt, teacher.Gender);
			sql.AddParameter("@CatetoryCode", SqlDbType.NVarChar, teacher.CatetoryCode);
			sql.AddParameter("@Age", SqlDbType.TinyInt, teacher.Age);
			sql.AddParameter("@Salary", SqlDbType.Decimal, teacher.Salary);
			sql.AddParameter("@Degree", SqlDbType.NVarChar, teacher.Degree);
			sql.AddParameter("@Record", SqlDbType.NVarChar, teacher.Record);
			sql.AddParameter("@Pic", SqlDbType.NVarChar, teacher.Pic);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertTeacher);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) teacher.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.Teacher teacher)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Code", SqlDbType.NVarChar, teacher.Code);
			sql.AddParameter("@Name", SqlDbType.NVarChar, teacher.Name);
			sql.AddParameter("@Gender", SqlDbType.TinyInt, teacher.Gender);
			sql.AddParameter("@CatetoryCode", SqlDbType.NVarChar, teacher.CatetoryCode);
			sql.AddParameter("@Age", SqlDbType.TinyInt, teacher.Age);
			sql.AddParameter("@Salary", SqlDbType.Decimal, teacher.Salary);
			sql.AddParameter("@Degree", SqlDbType.NVarChar, teacher.Degree);
			sql.AddParameter("@Record", SqlDbType.NVarChar, teacher.Record);
			sql.AddParameter("@Pic", SqlDbType.NVarChar, teacher.Pic);
			sql.AddParameter("@Id", SqlDbType.Int, teacher.Id);
			sql.ExecuteSql(SqlUpdateTeacher);
		}

		public void Delete(Core.Business.Teacher teacher)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, teacher.Id);
			sql.ExecuteSql(SqlDeleteTeacher);
		}

		public IList<Core.Business.Teacher> GetAllTeacher()
		{
			IList<Core.Business.Teacher> teacherlist = new List<Core.Business.Teacher>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllTeacher);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.Teacher teacher = new Core.Business.Teacher();

					if (!reader.IsDBNull(0)) teacher.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) teacher.Code = reader.GetString(1);
					if (!reader.IsDBNull(2)) teacher.Name = reader.GetString(2);
					if (!reader.IsDBNull(3)) teacher.Gender = reader.GetByte(3);
					if (!reader.IsDBNull(4)) teacher.CatetoryCode = reader.GetString(4);
					if (!reader.IsDBNull(5)) teacher.Age = reader.GetByte(5);
					if (!reader.IsDBNull(6)) teacher.Salary = reader.GetDecimal(6);
					if (!reader.IsDBNull(7)) teacher.Degree = reader.GetString(7);
					if (!reader.IsDBNull(8)) teacher.Record = reader.GetString(8);
					if (!reader.IsDBNull(9)) teacher.Pic = reader.GetString(9);

					teacher.MarkOld();
					teacherlist.Add(teacher);
				}
				reader.Close();
			}
			return teacherlist;
		}
		#endregion
	}
}
